#include <i2cmaster.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
lcd.begin(16,2);

lcd.setCursor(0,0);
lcd.print("initializing..");
// Serial.begin(9600);
// Serial.println("Hello!");
i2c_init(); //Initialise the i2c bus
// Serial.println("Return from i2c_init");
// PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups


}
void loop()
{
int dev = 0x5A<<1;
int data_low = 0;
int data_high = 0;
int pec = 0;
i2c_start_wait(dev+I2C_WRITE);
i2c_write(0x07);


i2c_rep_start(dev+I2C_READ);
data_low = i2c_readAck(); //Read 1 byte and then send ack
data_high = i2c_readAck(); //Read 1 byte and then send ack
pec = i2c_readNak();
i2c_stop();

//This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
double tempFactor = 0.02; // 0.02 degrees per LSB
double tempData = 0x0000;
int frac;

// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;
tempData = tempData - 273.15;
// Serial.print((int)tempData); //Print temp in degrees C to serial
// Serial.print(".");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temperature :");
lcd.setCursor(8,1);
lcd.print((int)tempData);
lcd.print(".");



tempData=tempData-(int)tempData;
frac=tempData*100;
// Serial.println(frac);
lcd.print(frac);
delay(1000);  
}
